using System;

namespace PDUConverter.SmartMessaging
{
    /// <summary>
    /// Implements an Application Port Addressing Information Element (16 bit address).
    /// </summary>
    /// <remarks>This element is used to indiate from which port a message 
    /// originated and to which port it should be directed to.</remarks>
    public class PortAddressElement : InformationElement
    {
        private byte[] destinationPort;
        private byte[] originatorPort;

        /// <summary>
        /// Initializes a new instance of the <see cref="T:GsmComm.PduConverter.SmartMessaging.PortAddressElement" /> class.
        /// </summary>
        /// <param name="destinationPort">The destination port as 4 hex characters, e.g. "1582".</param>
        /// <param name="originatorPort">The source port as 4 hex characters, e.g. "0000".</param>
        /// <exception cref="T:System.ArgumentException">destinationPort or originatorPort is not 4 characters long or contains non-hex characters.</exception>
        public PortAddressElement(string destinationPort, string originatorPort) : base(5)
        {
            if ((destinationPort.Length != 4) || !Calc.IsHexString(destinationPort))
            {
                throw new ArgumentException("destinationPort must be 4 characters long and must only consist of Hex characters.");
            }
            if ((originatorPort.Length != 4) || !Calc.IsHexString(originatorPort))
            {
                throw new ArgumentException("originatorPort must be 4 characters long and must only consist of Hex characters.");
            }
            this.destinationPort = Calc.HexToInt(destinationPort);
            this.originatorPort = Calc.HexToInt(originatorPort);
        }

        /// <summary>
        /// Returns the byte array equivalent of this instance.
        /// </summary>
        /// <returns>The byte array.</returns>
        public override byte[] ToByteArray()
        {
            return new byte[] { base.Identifier, 4, this.destinationPort[0], this.destinationPort[1], this.originatorPort[0], this.originatorPort[1] };
        }

        /// <summary>
        /// Gets the destination port.
        /// </summary>
        public byte[] DestinationPort
        {
            get
            {
                return this.destinationPort;
            }
        }

        /// <summary>
        /// Gets the originator port.
        /// </summary>
        public byte[] OriginatorPort
        {
            get
            {
                return this.originatorPort;
            }
        }
    }
}